import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Stack;

public class Test {

    public static boolean isValid(String s){
        Stack <Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if(ch=='('||ch=='{'||ch=='['){
                stack.push(ch);
            }
            else{
                if(stack.empty()){
                    return false;
                }
                char tmp=stack.peek();
                if(tmp=='('&&ch==')'||tmp=='['&&ch==']'||tmp=='{'&&ch=='}'){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }else {
            return true;
        }
    }
    public static void main3(String[] args) {
        MyStack myStack=new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(" ");
    }
    public static void main1(String[] args) {
        List <Integer> list1= new LinkedList<Integer>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);
        //System.out.println(list1);
    /*    for (Integer x:
             list1) {
            System.out.print(x+" ");
        }*/
        ListIterator<Integer>it= list1.listIterator();//迭代器
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println("");
        ListIterator<Integer>it2=list1.listIterator(list1.size());
        while(it.hasPrevious()){
            System.out.print(it.previous()+" ");
        }
    }
    public static boolean isOperation(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }
        else {
            return false;
        }
    }
    public static int evalRPN1(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for (String s:
             tokens) {
            if(!isOperation1(s)){
                stack.push(Integer.parseInt(s));
            }else{
                int num1=stack.pop();
                int num2=stack.pop();
                switch (s){
                    case"+":
                        stack.push(num2+num1);
                        break;
                    case"-":
                        stack.push(num2-num1);
                        break;
                    case"*":
                        stack.push(num2*num1);
                        break;
                    case"/":
                        stack.push(num2/num1);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public static int evalRPN(String[] tokens){
        Stack<Integer> stack= new Stack<>();
        for (String s:
             tokens) {
            if(!isOperation(s)){
                stack.push(Integer.parseInt(s));
            }else{
                int num1=stack.pop();
                int num2=stack.pop();
                switch (s){
                    case "+":
                        stack.push(num2+num1);
                        break;
                    case "-":
                        stack.push(num2-num1);
                        break;
                    case "*":
                        stack.push(num2*num1);
                        break;
                    case "/":
                        stack.push(num2/num1);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public static boolean isOperation1(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }else{
            return false;
        }
    }
    public static void main(String[] args) {
        //String s1="{[]}";
        //System.out.println(isValid(s1));
        String []s={"4","13","5","/","+"};
        System.out.println(evalRPN(s));
    }
}
